package com.example.sup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sup.entity.Prison;
import com.example.sup.entity.PrisonDistrict;
import com.example.sup.service.PrisonDistrictService;
import com.example.sup.mapper.PrisonDistrictMapper;
import com.example.sup.service.PrisonService;
import com.example.sup.vo.PrisonDistrictComboBoxVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author mac
* @description 针对表【prison_district(监区表)】的数据库操作Service实现
* @createDate 2024-07-19 10:05:17
*/
@Service
public class PrisonDistrictServiceImpl extends ServiceImpl<PrisonDistrictMapper, PrisonDistrict>
    implements PrisonDistrictService{

    @Autowired
    private PrisonDistrictService districtService;

    @Autowired
    private PrisonService prisonService;


    @Override
    public List<PrisonDistrictComboBoxVO> getPrisonDistrictsWithPrisons() {
        List<PrisonDistrict> prisonDistrictList = districtService.list();

        List<PrisonDistrictComboBoxVO> voList = new ArrayList<>();
        for (PrisonDistrict district : prisonDistrictList) {

            //4. 根据prisonDistrictId查询prison表数据
            Integer prisonDistrictId = district.getPrisonDistrictId();
            QueryWrapper<Prison> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("prison_district_id",prisonDistrictId);
            List<Prison> prisons = prisonService.list(queryWrapper);

            //5. 创建VO对象并填充数据
            PrisonDistrictComboBoxVO vo = new PrisonDistrictComboBoxVO();
            vo.setPrisonDistrictId(district.getPrisonDistrictId());
            vo.setPrisonDistrictName(district.getPrisonDistrictName());
            vo.setPrisonDistrictType(district.getPrisonDistrictType());
            vo.setReservedFields2(district.getReservedFields2());
            vo.setCreateTime(district.getCreateTime());
            vo.setPrisons(prisons);

            //6. 将VO添加到列表中
            voList.add(vo);
        }

        return voList;
    }
}




